#include "LoaderOFF.h"
#include <fstream>

using namespace std;

namespace glow
{

  MeshData *LoaderOFF::load(const string &filename)
  {
    MeshData * Mp = new MeshData(filename);

    ifstream file(filename.c_str(), ios::in);
    if(file)
      {
	std::string filetype;
	file >> filetype;

	if (filetype=="OFF")
	  {
	    unsigned int trashint = 0;
			
	    file >> Mp->_nb_vertices >> Mp->_nb_indices >> trashint;
			
	    /* tableau de points */
	    Mp->_vertices =new float[Mp->_nb_vertices*3];
	    Mp->_index=new unsigned int[Mp->_nb_indices*3];
			
	    for (unsigned int i = 0; i<Mp->_nb_vertices*3; i = i+3)
	      {
		file >> Mp->_vertices[i] >> Mp->_vertices[i+1] >> Mp->_vertices [i+2];
	      }

	    for (unsigned int j= 0 ;j<Mp->_nb_indices*3 ; j=j+3)
	      {
		file >> trashint >> Mp->_index[j] >> Mp->_index[j+1] >> Mp->_index[j+2];
	      }
	  }
	else 
	  {
	    std::cout<< "type de fichier non pris en charge"<<std::endl;
	  }
	file.close();
      }
    else
      std::cerr << "Impossible d'ouvrir le fichier !" << std::endl;
    return Mp;
  }
	
}
